import process from '@ohos.process';
import bundleManager from '@ohos.bundle.bundleManager';
import { preferences } from '@kit.ArkData';
import { common, Context } from '@kit.AbilityKit';
import fs from '@ohos.file.fs';
import { GlobalContext, GlobalContextConstants } from '../../common/GlobalContext';


export default class ApplicationManager {
  static pro = new process.ProcessManager();

  static exit() : void {
      ApplicationManager.pro.exit(0);
  }

  static getAppVersionCode() : string {
      let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT;
      return bundleManager.getBundleInfoForSelfSync(bundleFlags).versionCode.toString();
  }

  static getAppName() : string {
    let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION;
    let appInfo =  bundleManager.getBundleInfoForSelfSync(bundleFlags).appInfo;
    let context: common.UIAbilityContext =  GlobalContext.loadGlobalThis(GlobalContextConstants.ABILITY_CONTEXT)
    let appName = context.resourceManager.getStringSync(appInfo.labelId);
    return appName;
  }

  static getAAID() : string {
    return GlobalContext.loadGlobalThis(GlobalContextConstants.AAID);
  }

  static getBundleName() : string {
    let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT;
    let bundleInfo =  bundleManager.getBundleInfoForSelfSync(bundleFlags);
    return bundleInfo.name;
  }

  static declarePermission(permission:string) : boolean {
    let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION;
    let bundleInfo =  bundleManager.getBundleInfoForSelfSync(bundleFlags);
    return bundleInfo.reqPermissionDetails.findIndex((value:bundleManager.ReqPermissionDetail)=>{
      return value.name == permission;
    }) !=-1;
  }

  static getSandboxPath(pathName:string):string{
    let context: common.UIAbilityContext =  GlobalContext.loadGlobalThis(GlobalContextConstants.ABILITY_CONTEXT)
    switch (pathName){
      case 'bundle':
        return context == null ? "/data/storage/el1/bundle":context.bundleCodeDir;
      case 'files':
        return context == null ? "/data/storage/el2/base/haps/entry/files":context.filesDir;
      case 'cache':
        return context == null ? "/data/storage/el2/base/haps/entry/cache":context.cacheDir;
      default:
        return "";
    }
  }
  private static RAWFILE_RESOURCES_NAME:string = "rawfileResources";
  private static LAST_INSTALL_TIME:string = "lastInstallTime";
  /**
   * 清理从rawfile copy的资源文件
   */
  static clearAppCache(context:Context){
    let preference = preferences.getPreferencesSync(context,{name: ApplicationManager.RAWFILE_RESOURCES_NAME});
    let lastInstallTime = preference.getSync(ApplicationManager.LAST_INSTALL_TIME,0)
    let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT;
    let currentInstallTime =  bundleManager.getBundleInfoForSelfSync(bundleFlags).installTime;
    if(lastInstallTime != currentInstallTime){
      preference.putSync(ApplicationManager.LAST_INSTALL_TIME,currentInstallTime);
      preference.flush();
      let parentPath = context.filesDir+"/"+ApplicationManager.RAWFILE_RESOURCES_NAME;
      if(fs.accessSync(parentPath)){
        fs.rmdirSync(parentPath);
      }
      fs.mkdirSync(parentPath,true);

      let docPath = context.cacheDir+"/app_data";
      if(!fs.accessSync(docPath)){
        fs.mkdirSync(docPath,true);
      }
    }
  }
}